/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
 * Localization of jQuery Validate error messages.
 *
 */
$ = jQuery;
jQuery.extend(jQuery.validator.messages, {
    required: wpgform_script_vars.required,
    remote: wpgform_script_vars.remote,
    email: wpgform_script_vars.email,
    url: wpgform_script_vars.url,
    date: wpgform_script_vars.date,
    dateISO: wpgform_script_vars.dateISO,
    number: wpgform_script_vars.number,
    digits: wpgform_script_vars.digits,
    creditcard: wpgform_script_vars.creditcard,
    equalTo: wpgform_script_vars.equalTo,
    accept: wpgform_script_vars.accept,
    maxlength: jQuery.validator.format(wpgform_script_vars.maxlength),
    minlength: jQuery.validator.format(wpgform_script_vars.minlength),
    rangelength: jQuery.validator.format(wpgform_script_vars.rangelength),
    range: jQuery.validator.format(wpgform_script_vars.range),
    max: jQuery.validator.format(wpgform_script_vars.max),
    min: jQuery.validator.format(wpgform_script_vars.min),
});

//phongnd 2015/01/23: need datepicker 
$(document).ready(function () {
	 $("#ngay_dat_hang").datepicker({
		dateFormat: "dd-mm-yy",
		beforeShowDay: function(date) { 
			//http://stackoverflow.com/questions/24212239/datepicker-not-working-in-html
            return [true, '', ''];
		},
		// onSelect: function(dateText) {
			// return $.datepicker.parseDate( "yy-mm-dd", dateText );
		// },
	 });
	 
	 //validate for input #ma_don_hang
	 //nono ., .bind("invalid-form.validate", function(){})??
	 //ok , reset: validator.resetForm();
	var validator = $("#frm_order").validate({
		// debug: true,
		// errorContainer: $("#warning, #summary"),
		errorElement: "span",
		errorPlacement: function(error, element) {
			// error.appendTo(element.parent("li"));
			// $(element).parent("label").append( error );
			$(element).parents("li").next('li').append( error );
		},
		// success: function(label) { // ok!!!
			// label.text("ok!").addClass("success");  
		// },
		
		messages: {
			ma_don_hang: {
				required: " (Yêu cầu)",
				minlength: " (Nhập từ 3-15 ký tự)",
				maxlength: " (Nhập từ 3-15 ký tự)"
			},
		}
		
	 });
	 
});

/*!
 * Add 1 row inside table.
 * 
 * PhongND 2015/01/23
 */
function add_more_items(o){

	// alert ('asd');
	//render item 
	var str_html = '<tr>'
			+' <td><input type="text" name="ma_hang[]" class="ma_hang"> </input></td>'
			+' <td><input type="text" name="ten_hang[]" class="ten_hang"> </input></td>'
			+' <td><input type="text" name="so_luong[]" class="so_luong"> </input></td>'
			+' <td><input type="text" name="don_gia[]" class="don_gia"> </input></td>'
		+' </tr> ';
	
	//append to table-body
	var table = $(o).parents('table');
	table.children('tbody').append(str_html); // if jQuery append...
	
	
	
    return false;
}
